
const app = getApp();
const api = app.apiInstance
import { getUserInfo } from '../../utils/store';
Page({
  data: {
    currentStep: 0,
    tabStatus: [{
      label: '未填写',
      value: ''
    }, {
      label: '进行中',
      value: 'process'
    }, {
      label: '已完成',
      value: 'finish'

    }],
    windowWidth: 0,
    id: '',
    formData: {},
    readOnlyItem:{},
    isEdit:true,
    sapId: '',
    userInfo: {
      sapid: '',
      userName: getUserInfo().siocPYName,
      nickName: getUserInfo().siocCnName,
      createTime: app.moment().format('YYYY-MM-DD HH:mm:ss'),
    }

  },
  async loadData(id) {
    if (id) {
      const formData = await api.get(`/v1/openApi/resource/change/details/${id}`);
      this.setData({
        formData: formData.data
      })
      if(!this.data.isEdit){
        
        this.setData({
          readOnlyItem: formData.data.key.contentMap.queRen,
        })

      }
    }
  },
  onLoad(params) {
    console.log('params',params);
    this.setData({
      id: params.id,
      sapId:params.sapid||getUserInfo().siocSapNo,
      'userInfo.sapid':params.sapid||getUserInfo().siocSapNo,
      isEdit:params.isEdit=='false'?false:true
    })
    console.log('this.data',this.data);
    console.log('params')
    this.loadData(params.id);
    var systemInfo = wx.getSystemInfoSync();
    this.setData({
      windowWidth: systemInfo.windowWidth
    })

  },



  async submit() {

    if(!this.data.isEdit){
      wx.navigateBack({
        delta: 1
      });
      return;
    }

    const queRen = this.selectComponent("#queRen").data;
    console.log('1111',this.data.formData,this.data.sapId);
    const current = this.data.formData.value.find(item => item.auditor == this.data.sapId &&item.remark.indexOf('确认')>=0);
    console.log('current',current)


    if (!queRen.form.comfirmPass) {
      wx.showToast({
        title: '请填写确认状态',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (queRen.form.comfirmPass == '确认通过') {
      await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

      const content = Object.assign({}, {...this.data.formData.key.contentMap},{ queRen: { ...queRen.form, userInfo: this.data.userInfo, title: '确认表' } })
      console.log('content', content)
      const updateStatusParams = {
        id: this.data.id,
        sapid: this.data.sapId,
        data: Object.assign({},{...this.data.formData.key},{ content: JSON.stringify(content) }, { status: 'ToBeReviewed' })
      }
      console.log('111', {...updateStatusParams.data})
      //改变整个主数据
      await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, {...updateStatusParams.data});
      wx.showToast({
        title: '确认成功',
        icon: 'none',
        duration: 2000
      })
      wx.navigateTo({
        url: `/subPages/bianGengSq/bianGengSq`,
      })
      return;
    }
    else{
      current.status= 'Reject';
      await api.post(`/v1/openApi/resource/change/process/${this.data.sapId}`, {
       ...current
      });

      const content = Object.assign({}, {...this.data.formData.key.contentMap}, { queRen: { ...queRen.form, userInfo: this.data.userInfo.value, title: '确认表' } })
      console.log('content', content)
      const updateStatusParams = {
        id: this.data.id,
        sapid: this.data.sapId,
        data: Object.assign({},{...this.data.formData.key}, { content: JSON.stringify(content) }, { status: 'Reject' })
      }
      console.log('updateStatusParams', updateStatusParams)
      //改变整个主数据
      await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`,  {...updateStatusParams.data});
      wx.showToast({
        title: '确认成功',
        icon: 'none',
        duration: 2000
      })
      wx.navigateTo({
        url: `/subPages/bianGengSq/bianGengSq`,
      })
      return;
    }

  }

})
